﻿/*
 * Copyright 2009 José David Tuero Rodríguez

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace AtmosCreator
{
    /// <summary>
    /// Class used to control installed games
    /// </summary>
    class GamesControl
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public GamesControl()
        {

        }

        /// <summary>
        /// Get available games
        /// </summary>
        /// <returns></returns>
        public string[] GetGames()
        {
            string[] games = null;
            try
            {
                games = Directory.GetDirectories(AtmosCreator.Properties.Resources.GamesDirectory);
            }
            catch (Exception)
            {
                CreateDefaultGame();
                games = Directory.GetDirectories(AtmosCreator.Properties.Resources.GamesDirectory);
            }
            if (games.Length == 0)
            {
                CreateDefaultGame();
                games = Directory.GetDirectories(AtmosCreator.Properties.Resources.GamesDirectory);
            }
            char[] separators = { '\\' };
            for (int i = 0; i < games.Length; i++)
            {
                games[i] = games[i].Split(separators)[1];                
            }
            return games;
        }

        /// <summary>
        /// Create folder hierarchy for default game
        /// </summary>
        private void CreateDefaultGame()
        {
            Directory.CreateDirectory(AtmosCreator.Properties.Resources.DefaultGameDirectory);
            Configuration.CreateDefaultConfig(AtmosCreator.Properties.Resources.DafultGame);
            Directory.CreateDirectory(AtmosCreator.Properties.Resources.DefaultGameDirectory + "\\" + AtmosCreator.Properties.Resources.SongDirectory);
            Directory.CreateDirectory(AtmosCreator.Properties.Resources.DefaultGameDirectory + "\\" + AtmosCreator.Properties.Resources.SelectChosenVideoDirectory);
            Directory.CreateDirectory(AtmosCreator.Properties.Resources.DefaultGameDirectory + "\\" + AtmosCreator.Properties.Resources.StartVideoDirectory);
            Directory.CreateDirectory(AtmosCreator.Properties.Resources.DefaultGameDirectory + "\\" + AtmosCreator.Properties.Resources.EndVideoDirectory);
            Directory.CreateDirectory(AtmosCreator.Properties.Resources.DefaultGameDirectory + "\\" + AtmosCreator.Properties.Resources.ChosenVideoDirectory);
        }

        /// <summary>
        /// Check the folders of the game exists, and if not create them
        /// </summary>
        /// <param name="game"></param>
        public void CheckGameDirectory(string game)
        {
            if (!Directory.Exists(AtmosCreator.Properties.Resources.GamesDirectory + "\\" + game + "\\" + AtmosCreator.Properties.Resources.SongDirectory))
            {
                Directory.CreateDirectory(AtmosCreator.Properties.Resources.GamesDirectory + "\\" + game + "\\" + AtmosCreator.Properties.Resources.SongDirectory);
            }
            if (!Directory.Exists(AtmosCreator.Properties.Resources.GamesDirectory + "\\" + game + "\\" + AtmosCreator.Properties.Resources.SelectChosenVideoDirectory))
            {
                Directory.CreateDirectory(AtmosCreator.Properties.Resources.GamesDirectory + "\\" + game + "\\" + AtmosCreator.Properties.Resources.SelectChosenVideoDirectory);
            }
            if (!Directory.Exists(AtmosCreator.Properties.Resources.GamesDirectory + "\\"+game + "\\" + AtmosCreator.Properties.Resources.StartVideoDirectory))
            {
                Directory.CreateDirectory(AtmosCreator.Properties.Resources.GamesDirectory + "\\" + game + "\\" + AtmosCreator.Properties.Resources.StartVideoDirectory);
            }
            if (!Directory.Exists(AtmosCreator.Properties.Resources.GamesDirectory + "\\" + game + "\\" + AtmosCreator.Properties.Resources.EndVideoDirectory))
            {
                Directory.CreateDirectory(AtmosCreator.Properties.Resources.GamesDirectory + "\\" + game + "\\" + AtmosCreator.Properties.Resources.EndVideoDirectory);
            }
            if (!Directory.Exists(AtmosCreator.Properties.Resources.GamesDirectory + "\\" + game + "\\" + AtmosCreator.Properties.Resources.ChosenVideoDirectory))
            {
                Directory.CreateDirectory(AtmosCreator.Properties.Resources.GamesDirectory + "\\" + game + "\\" + AtmosCreator.Properties.Resources.ChosenVideoDirectory);
            }
        }
    }
}
